﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Member.Domain;
using WuDada.Core.Persistence.Ado;
using WuDada.Core.Persistence;
using NHibernate.Criterion;

namespace WuDada.Core.Member.Persistence
{
    public class MemberDao : AdoDao, IMemberDao
    {
        public INHibernateDao NHibernateDao { get; set; }                

        /// <summary>
        /// 新增Member
        /// </summary>
        /// <param name="memberVO">被新增的Member</param>
        /// <returns>新增後的Member</returns>
        public MemberVO CreateMember(MemberVO memberVO)
        {
            NHibernateDao.Insert(memberVO);

            return memberVO;
        }

        /// <summary>
        /// 取得Member By MemberId
        /// </summary>
        /// <param name="memberId">MemberId</param>
        /// <returns>Member</returns>
        public MemberVO GetMemberById(int memberId)
        {
            return NHibernateDao.GetVOById<MemberVO>(memberId);
        }

        /// <summary>
        /// 取得Member By LoginId
        /// </summary>
        /// <param name="loginId">LoginId</param>        
        /// <returns>Member</returns>
        public MemberVO GetMemberByLoginId(string loginId)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<MemberVO>();
            dCriteria.Add(Expression.Eq("LoginId", loginId));
            dCriteria.AddOrder(Order.Desc("CreateTime"));

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<MemberVO>(dCriteria, 0, 1)[0];
        }

        /// <summary>
        /// 刪除Member
        /// </summary>
        /// <param name="memberVO">被刪除的Member</param>
        public void DeleteMember(MemberVO memberVO)
        {
            NHibernateDao.Delete(memberVO);
        }

        /// <summary>
        /// 更新Member
        /// </summary>
        /// <param name="memberVO">被更新的Member</param>
        /// <returns>更新後的Member</returns>
        public MemberVO UpdateMember(MemberVO memberVO)
        {
            NHibernateDao.Update(memberVO);

            return memberVO;
        }

        /// <summary>
        /// 取得Member
        /// </summary>
        /// <param name="sortField">排序欄位</param>
        /// <param name="sortDesc">升降冪排序</param>
        /// <returns>Member清單</returns>
        public IList<MemberVO> GetMemberList(string sortField, bool sortDesc)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<MemberVO>();          

            if (!string.IsNullOrEmpty(sortField))
            {
                if (sortDesc)
                {
                    dCriteria.AddOrder(Order.Desc(sortField));
                }
                else
                {
                    dCriteria.AddOrder(Order.Asc(sortField));
                }
            }

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<MemberVO>(dCriteria);
        }

        /// <summary>
        /// 取得Member
        /// </summary>
        /// <param name="pageIndex">分頁索引</param>
        /// <param name="pageSize">分頁大小</param>
        /// <param name="sortField">排序欄位</param>
        /// <param name="sortDesc">升降冪排序</param>
        /// <returns>Member清單</returns>
        public IList<MemberVO> GetMemberList(int pageIndex, int pageSize, string sortField, bool sortDesc)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<MemberVO>();

            if (!string.IsNullOrEmpty(sortField))
            {
                if (sortDesc)
                {
                    dCriteria.AddOrder(Order.Desc(sortField));
                }
                else
                {
                    dCriteria.AddOrder(Order.Asc(sortField));
                }
            }

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<MemberVO>(dCriteria, pageIndex, pageSize);
        }

        /// <summary>
        /// 取得Member筆數
        /// </summary>
        /// <returns>Member清單</returns>
        public int CountMemberListByPostId()
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<MemberVO>();        

            return NHibernateDao.CountByDetachedCriteria(dCriteria);
        }

        /// <summary>
        /// 動態查詢筆數Member
        /// </summary>
        /// <param name="where">搜尋語法，用Where...order by ...</param>
        /// <returns>筆數</returns>
        public int CountMemberByWhere(string where)
        {
            return NHibernateDao.CountByWhere<MemberVO>(where);
        }

        /// <summary>
        /// 動態查詢Member
        /// </summary>
        /// <param name="where">搜尋語法，用Where...order by ...</param>
        /// <param name="pageIndex">分頁索引</param>
        /// <param name="pageSize">分頁大小</param>
        /// <returns>搜尋結果</returns>
        public IList<MemberVO> SearchMemberByWhere(string where, int pageIndex, int pageSize)
        {
            return NHibernateDao.SearchByWhere<MemberVO>(where, pageIndex, pageSize);
        }
    }
}
